package com.conversationboard.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.logger.LogMessageType;
import com.conversationboard.logger.Logger;
import com.conversationboard.model.User;
import com.conversationboard.site.Site;
import com.conversationboard.standalone.checkdb.FixUnansweredThreads;

@WebServlet(name = "AdminSortUnansweredControllerServlet", urlPatterns = "/Admin/AdminSortUnansweredControllerServlet")
public class AdminSortUnansweredControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -8876063531659656696L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			User user = User.get(request.getUserPrincipal().getName());

			if (!user.isSiteAdmin()) {
				return;
			}

			if (!Site.isShut()) {
				Logger.log(new Date(), "Can only sort unanswered when site is shut", user, LogMessageType.SYSTEM);
				return;
			}

			if (!request.getParameter("key").equals(request.getSession().getId())) {
				return;
			}

			FixUnansweredThreads.fixUnansweredThreads();

		} catch (Throwable e) {
			throw new ServletException(e);
		}
	}

}